#pragma once

#include <iostream>
#include <string>

using namespace std;

namespace key
{
	template<class K>
	struct BSTNode
	{
		K _key;
		BSTNode<K>* _left;
		BSTNode<K>* _right;

		BSTNode(const K& key)
			:_key(key)
			,_left(nullptr)
			,_right(nullptr)
		{

		}
	};

	template<class K>
	class BSTree
	{
	public:
		typedef BSTNode<K> Node;

		bool Insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
			}

			Node* cur = _root;
			Node* parent = nullptr;

			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					return false;
				}
			}

			cur = new Node(key);

			if (parent->_key > key)
			{
				parent->_left = cur;
			}
			else
			{
				parent->_right = cur;
			}

			return true;
		}

		bool Erase(const K& key)
		{
			Node* cur = _root;
			Node* parent = nullptr;

			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (parent == nullptr)
						{
							_root = cur->_right;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}

						delete cur;
						return true;
					}
					else if (cur->_right == nullptr)
					{
						if (parent == nullptr)
						{
							_root = cur->_left;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}

						delete cur;
						return true;
					}
					else
					{
						Node* mlparent = cur;
						Node* mostleft = cur->_right;

						while (mostleft->_left)
						{
							mlparent = mostleft;
							mostleft = mostleft->_left;
						}

						cur->_key = mostleft->_key;

						if (mlparent->_left == mostleft)
						{
							mlparent->_left = mostleft->_right;
						}
						else
						{
							mlparent->_right = mostleft->_right;
						}

						delete mostleft;
						return true;

					}
				}
			}
			return false;
		}

		bool Find(const K& key)
		{
			Node* cur = _root;

			while (cur)
			{
				if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else
				{
					return true;
				}
			}
			return false;
			
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

	private:

		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_InOrder(root->_left);

			cout << root->_key << " ";

			_InOrder(root->_right);
		}

		Node* _root = nullptr;
	};
}

namespace key_value
{
	template<class K,class V>
	struct BSTNode
	{
		K _key;
		V _value;
		BSTNode<K,V>* _left;
		BSTNode<K,V>* _right;

		BSTNode(const K& key,const V& value)
			:_key(key)
			,_value(value)
			, _left(nullptr)
			, _right(nullptr)
		{

		}
	};

	template<class K, class V>
	class BSTree
	{
	public:
		typedef BSTNode<K, V> Node;

		BSTree() = default;

		~BSTree()
		{
			Destroy(_root);
			_root = nullptr;
		}

		BSTree(const BSTree<K, V>& bst)
		{
			_root=Copy(bst->_root);
		}

		BSTree<K, V>& operator=(BSTree<K, V> bst)
		{
			std::swap(_root,bst._root);
			return *this;
		}

		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}

		bool Insert(const K& key,const V& value)
		{
			if (_root == nullptr)
			{
				_root = new Node(key,value);
			}

			Node* cur = _root;
			Node* parent = nullptr;

			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					return false;
				}
			}

			cur = new Node(key,value);

			if (parent->_key > key)
			{
				parent->_left = cur;
			}
			else
			{
				parent->_right = cur;
			}

			return true;
		}

		bool Erase(const K& key)
		{
			Node* cur = _root;
			Node* parent = nullptr;

			while (cur)
			{
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (parent == nullptr)
						{
							_root = cur->_right;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}

						delete cur;
						return true;
					}
					else if (cur->_right == nullptr)
					{
						if (parent == nullptr)
						{
							_root = cur->_left;
						}
						else
						{
							if (parent->_left == cur)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}

						delete cur;
						return true;
					}
					else
					{
						Node* mlparent = cur;
						Node* mostleft = cur->_right;

						while (mostleft->_left)
						{
							mlparent = mostleft;
							mostleft = mostleft->_left;
						}

						cur->_key = mostleft->_key;

						if (mlparent->_left == mostleft)
						{
							mlparent->_left = mostleft->_right;
						}
						else
						{
							mlparent->_right = mostleft->_right;
						}

						delete mostleft;
						return true;

					}
				}
			}
			return false;
		}

		Node* Find(const K& key)
		{
			Node* cur = _root;

			while (cur)
			{
				if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else
				{
					return cur;
				}
			}
			return nullptr;

		}


	private:
		Node* _root = nullptr;


		void Destroy(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			Destroy(root->_left);
			Destroy(root->_right);

			delete root;
		}

		Node* Copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}

			Node* newnode = new Node(root->_key,root->_value);
			newnode->_left = Copy(root->_left);
			newnode->_right = Copy(root->_right);

			return newnode;
		}

		void _InOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_InOrder(root->_left);

			cout << root->_key << " " << root->_value << endl;

			_InOrder(root->_right);
		}

	};
}